var geojsonLMT = {
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "properties": {
                "name": "HOME",
                "health": 0.2,
            },
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [108.571154, 31.028409],
                        [108.571128, 31.028355],
                        [108.571017, 31.028399],
                        [108.57105, 31.028477],
                        [108.571083, 31.028468],
                        [108.571069, 31.028436],
                    ]
                ]
            }
        },
        {
            "type": "Feature",
            "properties": {
                "name": "sheep_home",
                "health": 0.736965,
            },
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [108.570981445313, 31.028691677518],//right_bottom=>右
                        [108.570986, 31.028563],//left_top
                        [108.571119, 31.028583],//left_bottom
                        [108.571132269966, 31.028689236112],//right_top
                    ]
                ]
            }
        },
        {
            "type": "Feature",
            "properties": {
                "name": "RanJia_home",
                "health": 0.736965,
            },
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [108.571212, 31.026687],
                        [108.571297, 31.026812],
                        [108.571408, 31.026717],
                        [108.571319, 31.02661],
                    ]
                ]
            }
        },

    ]
}


//3D立面体绘制   橙色       粉色        灰色        紫色       深紫色     浅蓝        浅绿色      深灰       红色 
//health越接近1越靠近红色    
var colors = ['#fb8b05', '#f7cdbc', '#b89485', '#806d9e', '#74759b', '#93b5cf', '#55bb8a', '#80766e', '#f13c22'].reverse();
function topColor(index, feature) {
    var v = feature.properties.health * 100;
    return v < 40 ? colors[8] :
        v < 50 ? colors[7] :
            v < 55 ? colors[6] :
                v < 60 ? colors[5] :
                    v < 65 ? colors[4] :
                        v < 70 ? colors[3] :
                            v < 75 ? colors[2] :
                                v < 80 ? colors[1] :
                                    v < 100 ? colors[0] : '#8076a3';
}

function sideTopColor(index, feature) {
    var v = feature.properties.health * 100;
    //var v = feature.properties.h * 4;
    return v < 40 ? colors[8] :
        v < 50 ? colors[7] :
            v < 55 ? colors[6] :
                v < 60 ? colors[5] :
                    v < 65 ? colors[4] :
                        v < 70 ? colors[3] :
                            v < 75 ? colors[2] :
                                v < 80 ? colors[1] :
                                    v < 100 ? colors[0] : 'green';
}
function sideBottomColor(index, feature) {
    var v = feature.properties.health * 100;
    //var v = feature.properties.h * 4;
    return v < 40 ? colors[8] :
        v < 50 ? colors[7] :
            v < 55 ? colors[6] :
                v < 60 ? colors[5] :
                    v < 65 ? colors[4] :
                        v < 70 ? colors[3] :
                            v < 75 ? colors[2] :
                                v < 80 ? colors[1] :
                                    v < 100 ? colors[0] : 'green';
}
function getHeight(index, feature) {
    var v = feature.properties.health * 100;
    var height = [1, 2, 4, 6, 8, 10, 12, 14, 16];
    return v < 40 ? height[8] :
        v < 50 ? height[7] :
            v < 55 ? height[6] :
                v < 60 ? height[5] :
                    v < 65 ? height[4] :
                        v < 70 ? height[3] :
                            v < 75 ? height[2] :
                                v < 80 ? height[1] :
                                    v < 100 ? height[0] : 0;
}

var text = new AMap.Text({
    text: '文本标记',
    anchor: 'center', // 设置文本标记锚点
    draggable: true,
    cursor: 'pointer',
    angle: 0,
    visible: false,
    offset: [0, -25],
    style: {
        'padding': '5px 10px',
        'margin-bottom': '1rem',
        'border-radius': '.25rem',
        'background-color': 'rgba(0,0,0,0.5)',
        // 'width': '12rem',
        'border-width': 0,
        'box-shadow': '0 2px 6px 0 rgba(255, 255, 255, .3)',
        'text-align': 'center',
        'font-size': '16px',
        'color': '#fff',
    },
});

text.setMap(map);
var geojsonlmt = geojsonLMT;
var polygon_list = geojsonlmt.features;
var listpoly = [];
var vectorLayer = new AMap.VectorLayer();
map.add(vectorLayer);

//构建3D立面
function creatpoly() {
    if (geojsonlmt) {
        if (polygon_list && polygon_list.length > 0) {
            for (var item of polygon_list) {
                var roofColor = sideTopColor(1, item);
                var wallColor = sideBottomColor(1, item);
                var high = getHeight(1, item);
                let polyphy = new AMap.Polygon({
                    path: item.geometry.coordinates,
                    extrusionHeight: high,
                    roofColor: roofColor,
                    wallColor: wallColor,
                    //bubble: true,
                    //draggable: true,
                    zIndex: 50,
                    extData: item,
                })

                //鼠标从3D立面移开时事件

                polyphy.on('click', (event) => {
                    text.show();
                    var pickItem = polyphy.getOptions().extData;
                    var health = pickItem.properties.health;
                    var v = pickItem.properties.health * 100;
                    text.setText(pickItem.properties.name + ' Values:' + parseInt(health * 100) + ' %');
                    text.setPosition(event.lnglat);
                    for (var curpoly of listpoly) {
                        if (curpoly === polyphy) {
                            polyphy.setOptions({
                                roofColor: [164, 241, 199, 0.5],
                                wallColor: [164, 241, 199, 0.5],

                            })
                        } else {
                            curpoly.setOptions({
                                roofColor: topColor(1, curpoly.getOptions().extData),
                                wallColor: sideTopColor(1, curpoly.getOptions().extData),

                            })

                        }
                    }

                })
                //map.add(polyphy);
                vectorLayer.add(polyphy);
                listpoly.push(polyphy);
            }
        }

    }
}

//销毁3D立面体
function despoly() {
    text.hide();
    if (listpoly && listpoly.length > 0) {

        for (var i = 0; i < listpoly.length; i++) {
            var curpoly = listpoly[i];
            curpoly.destroy();
            delete curpoly;
            curpoly = null;
        }
        delete listpoly;
        listpoly = [];
    }

    if (map) {
        var layers = map.getLayers();
        if (layers && layers.length > 0) {
            for (var layer of layers) {
                if (layer.CLASS_NAME === "AMap.VectorLayer") {
                    layer.destroy();
                }
            }
        }
    }
}
creatpoly();//默认创建立面体